public class test {
    //leetcode 35.搜索插入位置
    class Solution {
        public int searchInsert(int[] nums, int num) {
            int left = 0;
            int right = nums.length - 1;
            while (left < right) {
                int mid = left + (right - left + 1) / 2;
                if (nums[mid] <= num) {
                    left = mid;
                } else if (nums[mid] > num) {
                    right = mid - 1;
                }
            }
            if(nums[left] < num) left++;
            return left;
        }
    }
    //leetcode 33.搜索旋转排序数组
    class Solution {
        public int search(int[] nums, int target) {
            int len = nums.length;
            int left = 0;
            int right = len - 1;
            while(left < right){
                int mid = left + (right - left) / 2;
                if(nums[mid] == target) return mid;
                //判断mid当前在哪一侧
                //mid在左侧
                if(nums[mid] > nums[len - 1]){
                    if(nums[0] <= target && nums[mid] > target){
                        right = mid - 1;
                    }else {
                        left = mid + 1;
                    }
                }
                //mid在右侧
                else {
                    if(nums[len - 1] >= target && nums[mid] < target){
                        left = mid + 1;
                    }else {
                        right = mid - 1;
                    }
                }
            }
            return nums[left] == target ? left : -1;
        }
    }
    //leetcode 153.寻找旋转排序数组中的最小值
    class Solution {
        public int findMin(int[] nums) {
            if (nums.length == 1) {
                return nums[0];
            }
            int len = nums.length;
            int left = 0;
            int right = len - 1;
            while (left < right) {
                int mid = left + (right - left) / 2;
                if(nums[mid] > nums[len - 1]){
                    left = mid + 1;
                }else {
                    right = mid;
                }
            }
            return nums[left];
        }
    }
    //leetcode 852.山脉数组的峰顶索引
    class Solution {
        public int peakIndexInMountainArray(int[] arr) {
            int left = 0;
            int right = arr.length - 2;
            while(left < right){
                int mid = left + (right - left) / 2;
                if(arr[mid] <= arr[mid + 1]){
                    left = mid + 1;
                }else {
                    right = mid;
                }
            }
            return left;
        }
    }
    //leetcode 162.寻找峰值
    class Solution {
        public int findPeakElement(int[] nums) {
            int left = 0;
            int right = nums.length - 1;
            while(left < right){
                int mid = left + (right - left) / 2;
                if(nums[mid] > nums[mid + 1]){
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }
            return left;
        }
    }
}
